//
//  DailyDietDetailViewController.m
//  SHHCPhone
//
//  Created by gfsh on 14-6-21.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "DailyDietDetailViewController.h"
#import "DailyDietDetailView.h"
#import "DailyDietDetailCell.h"
#import "DailyDietInfo.h"
#import "RegexKitLite.h"

@implementation DailyDietDetailViewController
{
    DailyDietDetailView *dddv;
    DietInfo *dietInfo;
}

- (id)initWithDietInfo:(DietInfo*)_dietInfo
{
    self = [super init];
    if (self) {
        dietInfo = _dietInfo;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    navigationBarView.title = dietInfo.foodName;
    [navigationBarView setNormalLeftButton];
    
    [self initContentView];
}

//初始化页面
- (void)initContentView
{
    dddv = [[DailyDietDetailView alloc]initWithFrame:CGRectMake(0, NAVIGATIONBAR_HEIGHT, CURRENT_CONTENT_WIDTH, CURRENT_CONTENT_HEIGHT-NAVIGATIONBAR_HEIGHT)];
    if (dietInfo) {
        if (dietInfo.picPath) {
            [dddv.dietIV setImageWithURL:[NSURL URLWithString:dietInfo.picPath]];
        }else{
            dddv.dietIV.image = [UIImage imageNamed:@"默认图片icon"];
        }
        
        NSMutableArray *cells = [NSMutableArray array];
        
        if (dietInfo.foodDetail&&dietInfo.amount) {
            DailyDietDetailCell *dddc = [[DailyDietDetailCell alloc]initWithFrame:CGRectMake(0, 0, CURRENT_CONTENT_WIDTH-13-23, 0/*88*/)];
            dddc.titleLab.text = @"主料辅料量";
            NSArray *amountNames = [dietInfo.foodDetail componentsSeparatedByString:@"|"];
            NSArray *amountValues = [dietInfo.amount componentsSeparatedByString:@"|"];
            NSMutableString *amountStr = [[NSMutableString alloc]initWithString:@""];
            for (NSString *name in amountNames) {
                NSInteger index = [amountNames indexOfObject:name];
                [amountStr appendString:[NSString stringWithFormat:@"%@：%@g\n",name,index<amountValues.count?amountValues[index]:@"0"]];
            }
            [dddc setCellFrame:amountStr];
            [cells addObject:dddc];
        }

        if (dietInfo.foodName||dietInfo.energy||dietInfo.protein||dietInfo.fat||dietInfo.CHO) {
            DailyDietDetailCell *dddc1 = [[DailyDietDetailCell alloc]initWithFrame:CGRectMake(0, 0, CURRENT_CONTENT_WIDTH-13-23, 0/*110*/)];
            dddc1.titleLab.text = @"营养标签";
            NSString *content1 = [NSString stringWithFormat:@"%@的能量标签     \n能量：%.1fcarl      \n蛋白质：%.1fcarl     \n脂肪：%.1fcarl      \n碳水化合物：%.1fcarl",dietInfo.foodName,[dietInfo.energy floatValue],[dietInfo.protein floatValue],[dietInfo.fat floatValue],[dietInfo.CHO floatValue]];
            [dddc1 setCellFrame:content1];
            [cells addObject:dddc1];
        }
        
        if (dietInfo.method) {
            DailyDietDetailCell *dddc2 = [[DailyDietDetailCell alloc]initWithFrame:CGRectMake(0, 0, CURRENT_CONTENT_WIDTH-13-23, 0/*120*/)];
            dddc2.titleLab.text = @"做法";
            
            NSArray *arr = [dietInfo.method componentsMatchedByRegex:@"(。[0-9].)" range:NSMakeRange(0, dietInfo.method.length)];
            
            for (NSString *str in arr) {
                NSString *sub = [str substringWithRange:NSMakeRange(1, str.length-1)];
                dietInfo.method = [dietInfo.method stringByReplacingOccurrencesOfString:sub withString:[NSString stringWithFormat:@"\n\n%@",sub]];
            }
            
            [dddc2 setCellFrame:dietInfo.method];
            [cells addObject:dddc2];
        }
        
        dddv.cells = cells;
    }
    [self.view addSubview:dddv];
}

- (void)leftButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
